<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增</title>
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}">
    <!--引入外部图标库-->
    <link rel="stylesheet" th:href="@{/lib/layui/myicons/iconfont.css}">
    <script th:src="@{/lib/layui/layui.js}"></script>
    <script th:src="@{/js/jquery.min.js}"></script>
    <style>
        .layui-form-pane .layui-form-text .layui-textarea {
            min-height: 80px;
            max-height: 80px;
        }
        .div_show{
            transform: translate(-2000px);
            transition: all 0.8s;
        }
        .div_hide{
            transform: translate(2000px);
            transition: all 1.5s;
        }
        .photo-viewer{
            margin: 50px 0;
            text-align: center;
        }
        .img-reveal{
            display: inline-block;
            margin: 0px 8px;
        }
        .form-info{
            margin: 15px;
            position: relative;
            overflow: hidden
        }
        @media screen and (-webkit-min-device-pixel-ratio:0) {
            .form-info{
                margin: 12px;
            }
        }
    </style>
</head>
<body style="height: 100%">
<!-- -------------------------------------------------------新增连锁店信息 Start------------------------------------------------------- -->
<!--该隐藏字段为了标识本次修改的内容是什么-->
<input type="hidden" id="add-type" th:value="${operationType}">
<!--表单-->
<form class="layui-form layui-form-pane form-info addForm" action="" add-url="addMultipleInfo" th:if="${operationType == 'multiple'}">
    <div id="div_info" style="width:100%">
        <div class="layui-form-item">
            <div class="layui-form-item">

                <label class="layui-form-label">店名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" lay-verify="required" placeholder="请输入店名" autocomplete="off"
                           class="layui-input">
                </div>

                <label class="layui-form-label">地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="address" lay-verify="required" placeholder="请输入地址" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">

                <label class="layui-form-label">店主名</label>
                <div class="layui-input-inline">
                    <input type="text" name="masterName" lay-verify="required" placeholder="请输入店主名" autocomplete="off"
                           class="layui-input">
                </div>

                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" lay-verify="required" placeholder="请输入联系电话" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">

                <label class="layui-form-label">营业状态</label>
                <div class="layui-input-inline">
                    <input type="text" name="business" lay-verify="required" placeholder="请输入营业状态" autocomplete="off"
                           class="layui-input">
                </div>

            </div>
        </div>
    </div>

    <div class="content-input" style="text-align: center">
        <input type="button" value="提交" name="submit" class="layui-btn addBtn">
        <input type="reset" class="layui-btn" value="清空">
    </div>
    <!-- -------------------------------------------------------新增连锁店信息 END------------------------------------------------------- -->
</form>



<!-- -------------------------------------------------------新增员工信息 Start------------------------------------------------------- -->
<!--表单-->
<form class="layui-form layui-form-pane form-info addForm" action="" add-url="addEmployeeInfo" th:if="${operationType == 'employee'}">
    <div id="div_info" style="width:100%">
        <div class="layui-form-item">
            <div class="layui-form-item">

                <label class="layui-form-label">员工姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" lay-verify="required" placeholder="请输入员工姓名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">

                <label class="layui-form-label">登录账号</label>
                <div class="layui-input-inline">
                    <input type="text" name="loginName" lay-verify="required" placeholder="请输入登录账号" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">

                <label class="layui-form-label">登录密码</label>
                <div class="layui-input-inline">
                    <input type="text" name="password" lay-verify="required" placeholder="请输入登录密码" autocomplete="off"
                           class="layui-input">
                </div>

            </div>
        </div>
    </div>
    <!--权限区分-->
    <input type="hidden" name="powerId" value="1">
    <div class="content-input" style="text-align: center;margin-top: 35px;">
        <input type="button" value="提交" name="submit" class="layui-btn addBtn">
        <input type="reset" class="layui-btn" value="清空">
    </div>
    <!-- -------------------------------------------------------新增员工信息 END------------------------------------------------------- -->
</form>


<script>
    layui.use(['form','upload'], function () {
        var $ = layui.jquery
            , layer = layui.layer
            , upload = layui.upload;

        //回车键关闭弹窗
        var flagMsg = true;
        //声明一个变量防止表单重复提交
        var lock = true;

        /**
         * 绑定回车事件
         */
        $(document).keypress(function (e) {
            if(((e.keyCode || e.which)==13) && flagMsg==true) {
                //判断锁，防止重复提交
                if(lock) {
                    addMethod();
                }
            }else{
                //如果当前有类似layer.alert的窗体，点击最上层的确定按钮，并且取消所有焦点
                if($('.layui-layer-btn0').length > 0){
                    $('.layui-layer-btn0').eq($('.layui-layer-btn0').length - 1)[0].click();
                    $("*").blur();
                }
            }
        });

        /**
         * 单击提交事件
         */
        $(".addBtn").click(function () {
            //判断锁，防止重复提交
            if(lock) {
                addMethod();
            }
        });

        /**
         * 添加数据Ajax请求方法
         */
        function addMethod(){
            //修改控制关闭弹窗的标记
            flagMsg = false;
            //修改锁标记
            lock = false;
            //获取表单数据
            var addData = $(".addForm").serialize();
            //判断是否为空
            var flag = verifyData($("#add-type").val());
            //获取提交的url
            var url = $(".addForm").attr("add-url");
            //ajax提交
            if(flag.trim().length == 0) {
                $.getJSON(
                    url,                        //路径
                    addData,                    //请求参数
                    function (rollData) {       //回调方法
                        if (rollData.result == "success") {
                            layer.confirm(rollData.msg,{
                                btn:['确定']  //按钮
                                , skin: 'layui-layer-lan'
                                , icon: 6
                            },function () {
                                window.parent.location.reload();    //刷新父页面
                            });
                        } else {
                            layer.confirm(rollData.msg,{
                                btn:['确定']  //按钮
                                , skin: 'layui-layer-lan'
                                , icon: 5
                                , anim: 6
                            },function () {
                                //如果失败，将锁标记和回车键判断标记全部重置
                                lock = true;
                                flagMsg = true;
                            });
                        }
                    }
                )
            }else{
                layer.open({
                    title: '错误消息'
                    , content: flag
                    , skin: 'layui-layer-lan'
                    , shade: 0
                    , icon: 5
                    , anim: 6
                    , closeBtn: 0
                });
                // 将提交锁重新设置为true
                lock = true;
            }
        }


        /**
         * 验证数据
         */
        function verifyData(formName){
            var result = "";
            switch(formName){
                case "multiple":        //验证连锁店信息输入是否合法完整
                    result = verifyMultipleInfo();
                    break;
                case "employee":        //验证员工信息输入是否合法完整
                    var form = $(".addForm");
                    var items = form.find("input[type='text']");
                    var flag = true;
                    $.each(items,function () {
                        if($(this).val().trim().length == 0){
                            flag = false;
                            return;
                        }
                    });
                    if(!flag){
                        return "请填写完整信息！";
                    }
                    break;
            }
            return result;
        }

        /**
         * 验证连锁店信息
         */
        function verifyMultipleInfo(){
            //获取表单
            var form = $(".addForm");
            var phoneVerify = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;  //手机号正则
            var planeVerify = /0\d{2,3}-\d{7,8}/;       //座机号正则
            //验证表单需要填写的字段
            var name = form.find($("input[name = 'name']")).val();      //获取姓名
            var phone = form.find($("input[name='phone']")).val();      //获取年龄
            var address = form.find($("input[name='address']")).val();      //获取地址
            var masterName = form.find($("input[name='masterName']")).val();      //获取店主名
            var business = form.find($("input[name='business']")).val();      //获取营业状态
            if(name.trim().length == 0){
                return "请填写店名";
            }
            if(phone.trim().length == 0){
                return "请填写联系电话";
            }
            if(address.trim().length == 0){
                return "请填写地址";
            }
            if(masterName.trim().length == 0){
                return "请填写店主名";
            }
            if(business.trim().length == 0){
                return "请填写营业状态";
            }
            if(!(phoneVerify.test(phone) || planeVerify.test(phone))){
                return "联系电话格式不正确";
            }
            return "";
        }

    })
</script>
</body>
</html>